﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFDrawingContext
{
    /// <summary>
    /// AGV.xaml 的交互逻辑
    /// </summary>
    public partial class AGV : UserControl
    {
        public List<MenuContentItem> MenuItems { get; set; }
        public AGV()
        {
            InitializeComponent();
            // 动态生成数据
            MenuItems = new List<MenuContentItem>
            {
                new MenuContentItem { MenuTitle = "测试1", ContentText = "这是内容 1" },
                new MenuContentItem { MenuTitle = "测试2", ContentText = "这是内容 2" },
                new MenuContentItem { MenuTitle = "测试3", ContentText = "这是内容 3" }
            };

            DataContext = this;
            MenuListBox.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
        }
        // 菜单选择改变事件
        //private void MenuListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (MenuListBox.SelectedIndex >= 0)
        //    {
        //        switch (MenuListBox.SelectedIndex)
        //        {
        //            case 0:
        //                ContentScrollViewer.ScrollToVerticalOffset(0);
        //                break;
        //            case 1:
        //                ContentScrollViewer.ScrollToVerticalOffset(Content1.ActualHeight);
        //                break;
        //            case 2:
        //                ContentScrollViewer.ScrollToVerticalOffset(Content1.ActualHeight + Content2.ActualHeight);
        //                break;
        //        }
        //    }
        //}
        // 内容滚动事件
        //private void ContentScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        //{
        //    double verticalOffset = ContentScrollViewer.VerticalOffset;

        //    if (verticalOffset < Content1.ActualHeight)
        //    {
        //        MenuListBox.SelectedIndex = 0;
        //    }
        //    else if (verticalOffset < Content1.ActualHeight + Content2.ActualHeight)
        //    {
        //        MenuListBox.SelectedIndex = 1;
        //    }
        //    else
        //    {
        //        MenuListBox.SelectedIndex = 2;
        //    }
        //}
        // 菜单选择改变事件
        private void MenuListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (MenuListBox.SelectedIndex >= 0)
            {
                double scrollOffset = 0;
                for (int i = 0; i < MenuListBox.SelectedIndex; i++)
                {
                    // 获取每个内容项的实际高度
                    var contentItem = ContentItemsControl.ItemContainerGenerator.ContainerFromIndex(i) as FrameworkElement;
                    if (contentItem != null)
                    {
                        scrollOffset += contentItem.ActualHeight;
                    }
                }
                ContentScrollViewer.ScrollToVerticalOffset(scrollOffset);
                ListBoxItem currentItem = (ListBoxItem)MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex);
                AnimateItem(currentItem, 20, 1);
            }
        }

        // 内容滚动事件
        private void ContentScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            double verticalOffset = ContentScrollViewer.VerticalOffset;
            double currentOffset = 0;
            for (int i = 0; i < MenuItems.Count; i++)
            {
                // 获取每个内容项的实际高度
                var contentItem = ContentItemsControl.ItemContainerGenerator.ContainerFromIndex(i) as FrameworkElement;
                if (contentItem != null)
                {
                    currentOffset += contentItem.ActualHeight;
                    if (verticalOffset < currentOffset)
                    {
                        MenuListBox.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void AnimateItem(ListBoxItem item, double offset, double duration)
        {
            TranslateTransform transform = (TranslateTransform)item.RenderTransform;
            DoubleAnimation animation = new DoubleAnimation
            {
                To = offset,
                Duration = TimeSpan.FromSeconds(duration),
                EasingFunction = new CubicEase { EasingMode = EasingMode.EaseOut }
            };
            Storyboard.SetTarget(animation, transform);
            Storyboard.SetTargetProperty(animation, new PropertyPath(TranslateTransform.YProperty));

            Storyboard storyboard = new Storyboard();
            storyboard.Children.Add(animation);
            storyboard.Begin();
        }

        private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
        {
            if (MenuListBox.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
            {
                for (int i = 0; i < MenuListBox.Items.Count; i++)
                {
                    ListBoxItem item = (ListBoxItem)MenuListBox.ItemContainerGenerator.ContainerFromIndex(i);
                    if (item != null)
                    {
                        item.RenderTransform = new TranslateTransform() { Y=0};
                    }
                }
            }
        }
    }
}
